<template>
	<view>

		<uni-popup ref="addStudent" type="bottom" @maskClick="close()">
			<view class="bg-white ">
				<uni-section title="添加学生" type="circle">
					<template v-slot:right>
						<view @click="close">关闭</view>
					</template>
				</uni-section>

				<view class="margin-xs padding-xs bg-orange radius">
					如果学生姓名非常常见,比如小明,请使用手机号精准搜索
				</view>

				<!-- 搜索框 -->


				<uni-forms class="margin-sm" label-width="150rpx" ref="form" :modelValue="from" :rules="rules">
					<uni-forms-item label="课包类型" required name="offlineCurriculumType">
						<uni-data-checkbox mode="button" disabled v-model="from.offlineCurriculumType"
							:localdata="dict.offline_curriculum_type"
							:map="{text:'dictName',value:'dictValue'}"></uni-data-checkbox>
					</uni-forms-item>
					<uni-forms-item label="排课类型" required name="isShiting">
						<uni-data-checkbox mode="button" v-model="from.isShiting" :localdata="dict.offline_class_type"
							:map="{text:'dictName',value:'dictValue'}"></uni-data-checkbox>
					</uni-forms-item>
					<uni-forms-item label="跟班类型" required name="longTerm">
						<uni-data-checkbox mode="button" v-model="from.longTerm" :localdata="dict.longTerm"
							:map="{text:'dictName',value:'dictValue'}"></uni-data-checkbox>
					</uni-forms-item>

					<uni-forms-item label="搜索客户" required name="customerId">
						<view class="" style="height: 100%;">
							{{customerInfo!=null?customerInfo.student.name:''}}
							<button v-if="customerInfo==null" class="cu-btn" @click="queryCustomer">搜索</button>
						</view>

					</uni-forms-item>

					<uni-forms-item label="合同信息" v-if="from.isShiting!=1" required name="vipContractId">
						<uni-data-select v-model="from.vipContractId" :localdata="vipContractList" placeholder="选择合同"
							change="change"></uni-data-select>
					</uni-forms-item>

				</uni-forms>
				<view class="padding-bottom-lg margin-top-lg" style="padding :0px 20rpx 50rpx 20rpx">

					<view style="display: flex;">
						<view style="flex: 3;" @click="save">
							<text class="login-btn cu-btn block bg-blue lg round">添加学生</text>
						</view>

					</view>
				</view>

			</view>
			<query ref="queryCustomer" @click="queryCustomer" @clickCustomer="setCustomer"></query>
		</uni-popup>

	</view>
</template>

<script>
	import query from "@/pages/marketing/customer/query.vue"
	import {
		isNull
	} from "@/utils/validate";
	import {
		doInsert,
		doUpdate
	} from "@/api/system/academicAffairsCenter/scheduleTimetable/SysScheduleTimetableManagementApi";
	import {
		getListByParentOrStudent
	} from "@/api/marketing/customer/CustomerManagementApi";
	import {
		getListById
	} from "@/api/vip/contract/VipContractManagementApi";

	export default {
		components: {
			query,
		},
		data() {
			return {
				title: '添加学生',
				customerList: [],
				queryForm: {
					pageNo: 1,
					pageSize: 100,
					createTime_ORDER: "DESC",
					offlineTimetableId_EQ: null
				},
				from: {
					isShiting: "1",
					longTerm: "2",
					offlineCurriculumType: null,
					vipContractId: null,
					izUsable: '1',
					ischaban: '',
				},
				customerInfo: null,
				dict: {},
				query: null,
				vipContractList: [],
				rules: {
					isShiting: {
						rules: [{
							required: true,
							errorMessage: '不能为空'
						}]
					},
					longTerm: {
						rules: [{
							required: true,
							errorMessage: '不能为空'
						}]
					},
					offlineCurriculumType: {
						rules: [{
							required: true,
							errorMessage: '不能为空'
						}]
					},
					vipContractId: {
						rules: [{
							required: true,
							errorMessage: '不能为空'
						}]
					},
					customerId: {
						rules: [{
							required: true,
							errorMessage: '不能为空'
						}]
					},
					offlineTimetableId: {
						rules: [{
							required: true,
							errorMessage: '不能为空'
						}]
					}

				},
			}
		},
		methods: {
			setCustomer(e) {
				this.from.customerId = e.id
				this.customerInfo = e
				this.getContract(e.id)
			},
			save() {

				this.$refs['form'].validate().then(async res => {
					if (!isNull(this.from.id)) {
						const {
							success,
							msg
						} = await doUpdate(this.from);
						if (success) {

						}
					} else {
						const {
							success,
							msg
						} = await doInsert(this.from);
						if (success) {

						}
					}

					this.close();

				})

			},
			open(data) {
				Object.assign(this.$data, this.$options.data());
				this.dict.offline_curriculum_type = this.$getDictList("offline_curriculum_type");
				this.dict.offline_class_type = this.$getDictList("offline_class_type");
				this.dict.longTerm = this.$getDictList("longTerm");
				this.dict.no_yes = this.$getDictList("no_yes");
				console.log(data.offlineCurriculumType);
				this.from.offlineCurriculumType = data.offlineCurriculumType
				this.from.offlineTimetableId = data.id
				this.$refs.addStudent.open()

			},
			/**
			 * 搜索客户
			 */
			queryCustomer() {
				this.$refs.queryCustomer.open()
			},
			async getContract(aaa) {

				let {
					data,
					msg
				} = await getListById({
					"id": aaa,
					"startus": 9,
					"curriculumType": this.from.offlineCurriculumType
				})


				console.log(data)
				if (data.length == 0) {
					uni.showToast({
						title: "该学生没有可用合同",
						icon: "none"
					})

				} else {
					this.vipContractList = data
					if (data[0].disabling == null || data[0].disabling == '') {
						this.from.vipContractId = data[0].id
					} else {
						uni.showToast({
							title: "合同与班级不匹配",
							icon: "none"
						})

					}
					for (let s of this.vipContractList) {
						s.text = '(剩余' + s.surplusAmount + '可补' + s.surplusReserveAmount + ')' + s.curriculumTitle
						s.value = s.id
						if (s.curriculumType != from.offlineCurriculumType) {
							s.disable = true
						}
					}




				}


			},
			/**
			 * 按条件查询客户
			 */
			async getListByParentOrStudent() {

				this.customerList = []
				const {
					data
				} = await getListByParentOrStudent({
					query: this.query,
					isVip: this.from.isShiting != '1' ? 1 : 0
				})

				this.customerList = data
				for (let s of this.customerList) {

					s.text = s.parentsInfo.phone + "   " + s.student.name
					s.value = s.id
				}
				console.log(this.customerList.length);
			},

			close() {
				Object.assign(this.$data, this.$options.data());
				this.$refs.addStudent.close()
				// uni.showTabBar();
				this.$emit('refresh')
			},
		}
	}
</script>

<style>
</style>